{% extends "ejeje/layout.html" %}


{%block title%}Søk{%endblock%}

{%block head%}
<link rel="stylesheet" type="text/css" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.3/themes/cupertino/jquery-ui.min.css">
<link rel="stylesheet" type="text/css" href="/media/css/ui.jqgrid.css">
<script type="text/javascript" src="/media/grid.locale-no.js"></script>
<script type="text/javascript">
	jQuery.jgrid.useJSON = true; // true=prefer builtin json parser
  jQuery.jgrid.no_legacy_api = false; // true=only use new api (>3.6) 
</script>
<script type="text/javascript" src="/media/jquery.jqGrid.min.js"></script>
<script type="text/javascript" src="/media/plugins/grid.addons.js"></script>
<script type="text/javascript">
/* <![CDATA[ */

jQuery.extend($.fn.fmatter , {
    ejejelink : function(cellvalue, options, rowdata) {
        if(cellvalue.substring(0,7) == 'http://' || cellvalue.substring(0,8) == 'https://') {
            return "<a class=btn href='"+cellvalue+"' title='Les dokumentet'>Les</a>";
        } else if(cellvalue.length === 0) {
            if(rowdata[1] === false) { // unntatt offentlighet
                return "<a class=btn href='/innsyn/ny/"+options.rowId+"' title='Be om innsyn i dette dokumentet'>Søk</a>";
            } else {
                return "<a class=btn href='/innsyn/ny/"+options.rowId+"' title='Dokumentet er unntatt offentlighet: "+rowdata[7]+"'>Begjær <i class=icon-eye-close></i></a>";
            }
        } else {
            return cellvalue;
        }
    }
    , ejeje_public : function(cellvalue, options, rowdata) {
        return (cellvalue) ? 'Ja' : 'Nei';
    }
});
jQuery.extend($.fn.fmatter.ejejelink, {
    unformat : function(cellvalue, options) {
    return jQuery(cellvalue).attr('href');
}
});
var sq;
function do_search() {
  try {
    sq[0].triggerSearch();
  } catch(e) {
    //console.error(e);
  }
}
jQuery(document).ready(function(){
  $("#ejsearch").data("searchstring", "");
  jQuery("#ejlist").jqGrid({
    url:'/finn.json',
    datatype: 'json',
    mtype: 'GET',
    colNames:['Dokument', 'Offentlig', 'Tittel', 'Organisasjon', 'Dato', 'Sakskompleks', 'Retning', 'Hjemmel'],
    colModel :[ 
      {name:'uri', index:'uri', sortable:false, classes:'type-uri', formatter:'ejejelink', width:100}, 
      {name:'offentlig', index:'offentlig', hidden:true, formatter:'ejeje_public'}, 
      {name:'tittel', index:'tittel', width:500}, 
      {name:'org', index:'organisasjon', width:250}, 
      {name:'dato', index:'dato', hidden:false, classes:'type-date', width:90}, 
      {name:'sakskompleks', index:'sakskompleks', hidden:false, width:250},
      {name:'retning', index:'retning', hidden:false, width:90},
      {name:'hjemmel', index:'lovreferanse', hidden:true}
    ],
    pager: '#ejpager',
    rowNum:40,
    rowList:[20,40,80],
    sortname: 'dato',
    sortorder: 'desc',
    viewrecords: true,
    footerrow: true,
    gridview: false, // has to be false for 'afterInsertRow' event to fire
    height: 'auto',
    autowidth: false,
    gridComplete: function() { 
      //jQuery('#ejlist a[rel*=facebox]').facebox(); 
    },
    ondblClickRow: function(pid, row, col, event) { 
        $("#myModalLabel").text("Journalpost #"+pid);
        $("#myModalLink").attr("href", "/innsyn/ny/"+pid);
        $("#myModal").removeData('modal');
        $("#myModal").modal({remote:'/detaljer/journalpost/'+pid});
    },
    afterInsertRow: function(rowid, rowdata, rowelem) { 
    },
    loadError : function(xhr,st,err) {
    	jQuery("#rsperror").html("Type: "+st+"; Response: "+ xhr.status + " "+xhr.statusText);
    }
    
  });
//    .jqGrid('navGrid', '#ejpager', {}, {}, {}, {}, {id:'searchbtn'})
//    .jqGrid('navButtonAdd',"#ejpager",{caption: "feed", buttonicon:"icon-signal-diag", id:"feedbtn", title:"Åpne søket som en Atom-feed"}); 
  sq = jQuery("#ejsearch").jqGrid('filterGrid','#ejlist', {
    gridModel: false,
    autosearch: true,
    enableSearch: true,
    enableClear: true,
    searchButton: 'Nå',
    clearButton: 'Tøm',
    buttonclass: 'btn',
    formclass: 'form-search',
    afterSearch: function() { 
      var s = jQuery("#sg_search").val();
      window.location.hash = "#s="+encodeURI(s);
      jQuery("#ejsearch").data("searchstring", s); },
    afterClear: function() { jQuery("#ejsearch").data("searchstring", ""); window.location.hash = ""; },
    filterModel: [{
               label: 'Søk etter: ',
               name: 'search',
               stype: 'text',
               sopt: {accessKey:'/', title:'Skriv inn et søkebegrep', class:"search-query"}
           }]

  });
  $(window).bind('hashchange', function() { // jquery-hashchange from benalman.com 
      if(window.location.hash.substring(0,3) == "#s=") {
        var searchstring = decodeURI(window.location.hash.substring(3));
        if(jQuery("#ejsearch").data("searchstring") != searchstring) {
          jQuery("#sg_search").val(searchstring);
          //sq[0].triggerSearch();
          window.setTimeout(do_search, 500);
        }
      }
  });
  $(window).trigger('hashchange'); // see if the page loaded with a hash
  $("#searchfeed")
    .appendTo("#ejpager_left")
    .attr({title:"Hent dette søket som en Atom-strøm (feed)"})
    .bind("click", function(ev) {
       var q = $("#sg_search").val();
       var s = (q.length) ? "?_search=true&search=" + encodeURI(q) : "";
       window.location.pathname = "/finn.atom" + s;
    });
  $("#searchagent")
    .appendTo("#ejpager_left")
    .attr({title:"Lagre dette søket og få tilsendt nye treff"})
    .bind("click", function(ev) {
       var q = $("#sg_search").val();
       if(!q) {
        $.facebox("Ingen søkestreng!");
        return false;
       }
       $.post("/agent.json", 
              {searchstring: q, makeagent: "1"},
              function(data, textStatus) {
                //console.log(data);
                if(data.agent_added === true) {
                 // agent added
                 $("<b>Søket er lagret</b>")
                   .appendTo("#ejpager_left")
                   .delay(1200)
                   .fadeOut("slow");
                }
              },
              "json"
              );
    });
}); /* ]]> */
</script>

{%endblock%}


{%block body%}

<h1 title="... i hvert fall når du bruker ejeje!">Elektronisk postjournal er jammen enkelt</h1>
<div id="content">
  <div id="ejsearch"></div>
  <table id="ejlist"></table>
  <div id="ejpager"></div>
</div>
<i class="icon-tasks" id="searchfeed"></i>
<i class="icon-time" id="searchagent"></i>

<!-- Modal -->
<div id="myModal" class="modal hide fade" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
  <div class="modal-header">
    <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
    <h3 id="myModalLabel">Detaljer om journalpost</h3>
  </div>
  <div class="modal-body">
    <p>One fine body…</p>
  </div>
  <div class="modal-footer">
    <button class="btn" data-dismiss="modal" aria-hidden="true">Lukk</button>
    <a href="" id="myModalLink" class="btn btn-primary">Begjær innsyn</a>
  </div>
</div>
{%endblock%}


